﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml;

namespace Catsland.Core
{
    public class Animation
    {
        public Dictionary<string, AnimationClip> _animationClips;
		public Point _tiltUV { set; get; }
		public string _defaultAnimationClipName;
		public bool _isAutoPlay { set; get; }
        public int _millionSecondPerFrame { set; get; }

		public Animation(bool isAutoPlay = true, int millionSecondPerFrame = 50)
		{
			_isAutoPlay = isAutoPlay;
			_millionSecondPerFrame = millionSecondPerFrame;
			_tiltUV = new Point(1, 1);
			_defaultAnimationClipName = "";
			_animationClips = new Dictionary<string, AnimationClip>();
		}

        public void addAnimationClip(AnimationClip animationClip)
        {
            if (_animationClips == null)
            {
                _animationClips = new Dictionary<String, AnimationClip>();
            }
            _animationClips.Add(animationClip._name, animationClip);
        }

        public AnimationClip getAnimationClip(String name)
        {
            if (_animationClips == null || !_animationClips.ContainsKey(name))
            {
                return null;
            }
            return _animationClips[name];
        }

		public bool SaveToNode(XmlNode node, XmlDocument doc)
		{
			XmlElement animation = doc.CreateElement("Animation");
			node.AppendChild(animation);

			// tilt UV
			XmlElement tiltUV = doc.CreateElement("tiltUV");
			animation.AppendChild(tiltUV);
			tiltUV.SetAttribute("U", "" + _tiltUV.X);
			tiltUV.SetAttribute("V", "" + _tiltUV.Y);

			// default clip name
			XmlElement defaultClip = doc.CreateElement("defaultAnimationClipName");
			animation.AppendChild(defaultClip);
			defaultClip.InnerText = _defaultAnimationClipName;

			// is auto play
			XmlElement isAutoPlay = doc.CreateElement("isAutoPlay");
			animation.AppendChild(isAutoPlay);
			isAutoPlay.InnerText = "" + _isAutoPlay;

			// million second per frame
			XmlElement MSPF = doc.CreateElement("millionSecondPerFrame");
			animation.AppendChild(MSPF);
			MSPF.InnerText = "" + _millionSecondPerFrame;

			// clips
			XmlElement clips = doc.CreateElement("AnimationClips");
			animation.AppendChild(clips);

			foreach (KeyValuePair<String, AnimationClip> pair in _animationClips)
			{
				pair.Value.SaveToNode(clips, doc);
			}

			return true;
		}

		public static Animation LoadFromNode(XmlNode node, Scene scene)
		{
			XmlElement animation = (XmlElement)node;

			// tiltUV
			XmlElement tiltUV = (XmlElement)animation.SelectSingleNode("tiltUV");
			Point newTiltUV = new Point(int.Parse(tiltUV.GetAttribute("U")),
									int.Parse(tiltUV.GetAttribute("V")));
			
			// default clip name
			XmlElement defaultClipName = (XmlElement)animation.SelectSingleNode("defaultAnimationClipName");
			String newDefaultClipName = defaultClipName.InnerText;

			// is auto play
			XmlElement isAutoPlay = (XmlElement)animation.SelectSingleNode("isAutoPlay");
			bool newIsAutoPlay = bool.Parse(isAutoPlay.InnerText);

			// MSPF
			XmlElement MSPF = (XmlElement)animation.SelectSingleNode("millionSecondPerFrame");
			int newMSPF = int.Parse(MSPF.InnerText);

			Animation newAnimation = new Animation();
			newAnimation._tiltUV = newTiltUV;
			newAnimation._defaultAnimationClipName = newDefaultClipName;
			newAnimation._isAutoPlay = newIsAutoPlay;
			newAnimation._millionSecondPerFrame = newMSPF;

			// clips
			XmlNode clips = animation.SelectSingleNode("AnimationClips");
			foreach (XmlNode clip in clips.ChildNodes)
			{
				AnimationClip newClip = AnimationClip.LoadFromNode(clip, scene);
				newAnimation.addAnimationClip(newClip);
			}

			return newAnimation;
		}
	}
}
